package com.iotteam.androidbase.commen.network.onhttp;

import com.iotteam.androidbase.BuildConfig;
import com.iotteam.androidbase.base.BaseApplication;
import com.iotteam.androidbase.commen.network.NetworkUtils;
import com.iotteam.androidbase.commen.network.core.HttpListener;
import com.iotteam.androidbase.commen.network.core.INetworkWrapper;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.OkHttpNetworkExecutor;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.cache.DBCacheStore;
import com.yanzhenjie.nohttp.cookie.DBCookieStore;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.rest.RestRequest;
import com.yanzhenjie.nohttp.rest.StringRequest;

import java.util.Map;

/**
 * Created by lidechen on 5/4/17.
 */

public class NohttpWrapper implements INetworkWrapper {

    private RequestQueue mQueue;

    private static NohttpWrapper sInstance = new NohttpWrapper();
    private NohttpWrapper(){
        mQueue = NoHttp.newRequestQueue(6);
    }

    public static NohttpWrapper getInstance(){
        return sInstance;
    }

    @Override
    public void init() {
        Logger.setDebug(BuildConfig.DEBUG);// 开启NoHttp的调试模式, 配置后可看到请求过程、日志和错误信息。
        Logger.setTag("NoHttpSample");// 设置NoHttp打印Log的tag。

        // 一般情况下你只需要这样初始化：
//        NoHttp.initialize(this);

        // 如果你需要自定义配置：
        NoHttp.initialize(BaseApplication.getAppContext(), new NoHttp.Config()
                // 设置全局连接超时时间，单位毫秒，默认10s。
                .setConnectTimeout(30 * 1000)
                // 设置全局服务器响应超时时间，单位毫秒，默认10s。
                .setReadTimeout(30 * 1000)
                // 配置缓存，默认保存数据库DBCacheStore，保存到SD卡使用DiskCacheStore。
                .setCacheStore(
                        new DBCacheStore(BaseApplication.getAppContext()).setEnable(true) // 如果不使用缓存，设置setEnable(false)禁用。
                )
                // 配置Cookie，默认保存数据库DBCookieStore，开发者可以自己实现。
                .setCookieStore(
                        new DBCookieStore(BaseApplication.getAppContext()).setEnable(false) // 如果不维护cookie，设置false禁用。
                )
                // 配置网络层，默认使用URLConnection，如果想用OkHttp：OkHttpNetworkExecutor。
                .setNetworkExecutor(new OkHttpNetworkExecutor())
        );
    }

    private <T> void request(Map<String, String> params, Request<T> request, HttpListener<T> callback) {
        request.setCancelSign(params);

        //校验参数
        request.add("signature", NetworkUtils.generateSign(params));
        request.addHeader("charset","utf-8");

        mQueue.add(0, request, new HttpResponseListener<>(request, callback));
    }

    private void cancelRequest(Map<String, Object> params){
        mQueue.cancelBySign(params);
    }

    public void cancelAll(){
        mQueue.cancelAll();
    }

    @Override
    public <T> void postBean(String url, Map<String, String> params, Class<T> clazz, HttpListener<T> listener) {
        JavaBeanRequest<T> request = new JavaBeanRequest(url, clazz);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            request.add(entry.getKey(), entry.getValue());
        }
        request(params, request, listener);
    }

    @Override
    public void postString(String url, Map<String, String> params, HttpListener<String> listener) {
        RestRequest<String> request = new StringRequest(url, RequestMethod.POST);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            request.add(entry.getKey(), entry.getValue());
        }
        request(params, request, listener);
    }

    @Override
    public <T> void getBean(String url, Map<String, String> params, Class<T> clazz, HttpListener<T> listener) {
        JavaBeanRequest<T> request = new JavaBeanRequest(url, RequestMethod.GET, clazz);
        for (Map.Entry<String, String> entry : params.entrySet()) {
            request.add(entry.getKey(), entry.getValue());
        }
        request(params, request, listener);
    }

    @Override
    public void cancelPostBean(Map params) {
        cancelRequest(params);
    }
}
